package com.earthea.common.system.server;

import cn.hutool.core.io.unit.DataSizeUtil;
import cn.hutool.system.oshi.OshiUtil;
import com.earthea.common.util.ArithUtils;
import oshi.hardware.GlobalMemory;

/**
 * 內存相关信息
 */
public class Mem {
    /**
     * 内存总量
     */
    private final long total;

    /**
     * 已用内存
     */
    private final long used;

    /**
     * 剩余内存
     */
    private final long free;

    public Mem() {
        GlobalMemory memory = OshiUtil.getMemory();
        this.total = memory.getTotal();
        this.free = memory.getAvailable();
        this.used = memory.getTotal() - memory.getAvailable();
    }

    public String getTotal() {
        return DataSizeUtil.format(total);
    }

    public String getUsed() {
        return DataSizeUtil.format(used);
    }

    public String getFree() {
        return DataSizeUtil.format(free);
    }

    public double getUsage() {
        return ArithUtils.mul(ArithUtils.div(used, total, 4), 100);
    }
}
